package com.hylanda.processor.database.util;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;


/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2017年9月14日 下午2:07:46 
 * note
 */
public abstract class DatabaseAbstractHelper {
		
		// 返回Statement对象
		public static Statement getStatement(Connection con) {
			Statement stmt = null;
			try {
				stmt = con.createStatement();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return stmt;
		}

			// 返回preparedStatement对象
		public static PreparedStatement getPrepareStatement(Connection con,
				String sql) {
			PreparedStatement pst = null;
			try {
				pst = con.prepareStatement(sql);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return pst;
		}
		
		
		// 返回callableStatement对象
		public static CallableStatement getcallableStatement(Connection con,
				String sql,String inpara1,String inparam2,Integer outpara1,Integer outpara2) {
			CallableStatement cs = null;
			try {
				cs = con.prepareCall(sql);
				cs.setString(1, inpara1);
				cs.registerOutParameter(2, outpara1); 
//					cs.registerOutParameter(1, Types.INTEGER);//设置返回值类型
				cs.execute();
				//slq语句例子"{ ? = call snuffed_it_when(?) }"   "{ call set_death_age(?, ?) }"
				//SELECT id INTO poet_id FROM poets WHERE name = $1; 存储过程参数   http://blog.csdn.net/stevene/article/details/594959	
//					cs.setString(1, x);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return cs;
		}

		// 执行传过来的SQL语句，并返回一个ResultSet对象
		public static ResultSet getResultment(PreparedStatement pst) {
			ResultSet rs = null;
			try {
				rs = pst.executeQuery();
			} catch (SQLException e) {
				if(! (e instanceof MySQLSyntaxErrorException))
					e.printStackTrace();
			}
			return rs;
		}
		public static int getUpdateResultment(PreparedStatement pst) {
			int rs = 0;
			try {
				rs = pst.executeUpdate();
			} catch (SQLException e) {
				if(! (e instanceof MySQLSyntaxErrorException))
					e.printStackTrace();
			}
			return rs;
		}
		// 执行传过来的SQL语句，并返回一个ResultSet对象
		public static boolean getAmdResultment(PreparedStatement pst) {
			boolean rs = false;
			try {
				rs = pst.execute();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return rs;
		}
		public static ResultSet getResultment(CallableStatement cs) {
			ResultSet rs = null;
			try {
				rs = cs.executeQuery();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return rs;
		}
		
		public static ResultSet getResultment(Statement st,String sql) {
			ResultSet rs = null;
			try {
				rs = st.executeQuery(sql);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return rs;
		}
		public static void closeResultSet(ResultSet rs) {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

		public static void closePreparedStatement(PreparedStatement stmt) {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		public static void closeStatement(Statement st) {
			if (st != null) {
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		public static void closeCallableStatement(CallableStatement cs) {
			if (cs != null) {
				try {
					cs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		public static void closeConnection(Connection conn) {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		
		
}
